package handler

import (
	"common"
	"common/biz"
	"common/logs"
	"context"
	"core/repo"
	"core/service"
	"encoding/json"
	"framework/remote"
	"game_flee/logic"
	"game_flee/models/request"
	hall "hall/handler"
	"strconv"
)

type BigFleeKillHandler struct {
	gameService *service.GameService
	fm          *logic.BigFleeKillManager
	base        *hall.BaseHandler
}

func (h *BigFleeKillHandler) FleeRoomList(session *remote.Session, msg []byte) any {
	logs.Info("[Game] FleeRoomList msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] FleeRoomList err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CreateRoomReq
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Game]用户%d FleeRoomList err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	userData, err := h.gameService.GameFleeInfo(ctx, uint(uid))
	if err != nil {
		return common.F(err)
	}
	roomList := h.fm.GetRoom(session, userData)
	return common.S(roomList)
}

func (h *BigFleeKillHandler) FleeBetting(session *remote.Session, msg []byte) any {
	logs.Info("[Game] FleeBetting msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] FleeBetting err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.RoomFleeBetting
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Game]用户%d FleeBetting err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	userData, err := h.gameService.GameFleeInfo(ctx, uint(uid))
	if err != nil {
		return common.F(err)
	}
	var res any
	res, err = h.fm.RoomBetting(session, &req, userData)
	if err != nil {
		return common.F(err)
	}
	return common.S(res)
}

func (h *BigFleeKillHandler) FleeChangeRoom(session *remote.Session, msg []byte) any {
	logs.Info("[Game] FleeChangeRoom msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] FleeChangeRoom err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.RoomChangeRoom
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Game]用户%d FleeChangeRoom err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	userData, err := h.gameService.GameFleeInfo(ctx, uint(uid))
	if err != nil {
		return common.F(err)
	}
	var res any
	res, err = h.fm.FleeChangeRoom(session, &req, userData)
	if err != nil {
		return common.F(err)
	}
	return common.S(res)
}

func (h *BigFleeKillHandler) FleeOut(session *remote.Session, msg []byte) any {
	logs.Info("[Game] FleeOut msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] FleeOut err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	userData, err := h.gameService.GameFleeInfo(ctx, uint(uid))
	if err != nil {
		return common.F(err)
	}
	h.fm.FleeOut(userData)
	return common.S(nil)
}

func (h *BigFleeKillHandler) FleeLog(session *remote.Session, msg []byte) any {
	logs.Info("[Game] FleeBetting msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] FleeBetting err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.PageMsg
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Game]用户%d FleeBetting err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	data := h.fm.FleeLog(ctx, uint(uid), &req)
	return common.S(data)
}

func NewBigFleeKill(r *repo.Manager, fm *logic.BigFleeKillManager) *BigFleeKillHandler {
	h := &BigFleeKillHandler{
		gameService: service.NewGameService(r),
		fm:          fm,
		base:        hall.NewBaseHandler(r),
	}
	return h
}
